package nakedb.sql2xml;

import java.util.Random;

/**
 * Model of a SQL Table.
 * The Foreign keys are not stored here.
 * 
 * @author Ro
 */
public class Table {
  private String name;
  private String sql;
  
  /**
   * Creates a new table.
   * 
   * @param name The name of the table.
   */
  public Table(String name) {
    this.name = name;
    this.sql = "empty";
  }
  
  /**
   * Converts the Table into XML.
   * 
   * @return The XML representation of the Table.
   */
  public String toXmlNode() {
    StringBuilder buffer = new StringBuilder();
    Random rand = new Random();
    String sql = "aa";
    for (int i = 0; i<rand.nextInt(5); i++) {
      sql += " - " +new Character((char)(65 + rand.nextInt(25))).toString();
    }
    buffer.append(String.format("<node id=\"%s\">\n", name));
    buffer.append(String.format("  <data key=\"name\">%s</data>\n", name));
    buffer.append(String.format("  <data key=\"group\">%s</data>\n", new Character((char)(65 + rand.nextInt(7))).toString()));
    buffer.append(String.format("  <data key=\"traffic\">%s</data>\n", rand.nextInt(5)));
    buffer.append(String.format("  <data key=\"lines\">%s</data>\n", (1 + rand.nextInt(5)) * 1000));
    buffer.append(String.format("  <data key=\"attributes\">==============%s</data>\n", sql));
    buffer.append(String.format("  <data key=\"type\">%s</data>\n", rand.nextInt(10)));
    buffer.append(String.format("  <data key=\"critical\">%s</data>\n", rand.nextInt(5)));
    buffer.append(String.format("  <data key=\"repair\">%s</data>\n", rand.nextInt(2)));
    buffer.append(String.format("  <data key=\"traffic\">%s</data>\n", rand.nextInt(5)));
    buffer.append("</node>\n");

    return buffer.toString();
  }   

  /**
   * @return The name of the table.
   */
  public String getName() {
    return name;
  }
}
